from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from p7day01app.models import MailModel,UserModel
# Create your views here.
# 注册
class RegisterView(APIView):
    def post(self,request):
        user = request.data.get("user")
        password = request.data.get("password")
        tel = request.data.get("tel")
        m_user = UserModel.objects.filter(user=user)
        m_tel = UserModel.objects.filter(tel=tel)
        if m_user:
            return Response("用户名已存在")
        if password == "":
            return Response("密码不能为空")
        if m_tel:
            return Response("手机号已存在")
        UserModel.objects.create(
            user=user,
            password=password,
            tel=tel
        )
        return Response(True)
# 登录
class LoginView(APIView):
    def post(self,request):
        user = request.data.get("user")
        m_user = UserModel.objects.filter(user=user).first()
        if m_user:
            if m_user.password == request.data.get("password"):
                return Response(True)
            return Response("密码错误")
        return Response("用户名不存在")

# 绑定
class BangView(APIView):
    def post(self,request):
        e_mail = request.data.get("e_mail")
        if e_mail == "":
            return Response("邮箱不能为空")
        MailModel.objects.create(
            e_mail=e_mail
        )
        return Response(True)

# 修改密码
# class XiuView(APIView):
#     def put(self,request):
#         password = request.data.get("password")
#         UserModel.objects.